package com.demo.thread.semaphore;

/**
 * @author : YU.TAN
 * @date : 2021/11/8 11:18
 * 在计数型信号量中，信号的数量是没有限制的
 * 。换句话说，所有的线程都可以发送信号。与此不同的是，在边界型信号量中，通过bound字段增加了信号量的限制。
 */
public class BoundedSemaphore {
    private int signal = 0;
    private int bound = 0;

    public BoundedSemaphore(int signal, int bound) {
        this.signal = signal;
        this.bound = bound;
    }

    public synchronized void take() throws InterruptedException {
        while (this.signal==bound){
            wait();
        }
        this.signal++;
        this.notify();
    }

    public synchronized void release() throws InterruptedException {
            while (this.signal==0){
                wait();
            }
            this.signal--;
    }
}
